<?php
set_time_limit(0);
if (!defined('IN_XLP')) {
    exit('Access Denied!');
}


/**
 * Description of yunying
 * 运营商信息查询接口
 * @author hsisme
 */
class yunying {

    public static $errInfo = null;
    public static $config = array();
    public static $partnerCode = 'zixinjie_mohe';
    public static $partnerKey = '982a7eb30878469f82772161c53f8f39';

   
    static public function createTask($objData) {
        $url = "https://api.shujumohe.com/octopus/task.unify.create/v3?partner_code=".self::$partnerCode."&partner_key=".self::$partnerKey;
        $param['channel_type'] = 'YYS';
        $param['channel_code'] = '100000';
        $param['real_name']    = $objData['username'];
        $param['identity_code']= $objData['card_num'];
        $param['user_mobile']  = $objData['phone'];
        $result = getHttp($url , $param);
        $result = json_decode($result , true);
        if($result['code'] == 0){
            return $result['task_id'];
        }else{
            self::$errInfo = $result['message'];
            return false;
        }
    }




    static public function Login($objData) {
        $url = "https://api.shujumohe.com/octopus/yys.unify.acquire/v3?partner_code=".self::$partnerCode."&partner_key=".self::$partnerKey;
        $param['task_id']      = $objData['task_id'];
        $param['user_name']    = $objData['phone'];
        $param['user_pass']    = $objData['password'];
        $param['task_stage']   = 'INIT';
        $param['request_type'] = 'submit'; //第一次调用
        $result = self::Query($url , $param);
        // error_log(var_export($result, true)."\n", 3, "D:\ome3.log");
        if(!is_array($result)){
            self::$errInfo = $result;
            return false;
        }else{
            return $result;
        }
    }



    static public function Query($url , $objData) {
        $result = getHttp($url,$objData);
        $result = json_decode($result , true);
        if($result['code'] == 105 || $result['code'] == 137 || $result['code'] == 2007){
            // error_log(var_export($result, true)."\n", 3, "D:\ome1.log");
            return $result;
        }elseif($result['code'] == 100){
            $objData['request_type'] = 'query';
            // error_log(var_export($result, true)."\n", 3, "D:\ome2.log");
            return self::Query($url,$objData);
        }else{
            return $result['message'];
        }
    }




    //提交验证码
    static public function checkCode($data) {
        $url = "https://api.shujumohe.com/octopus/yys.unify.acquire/v3?partner_code=".self::$partnerCode."&partner_key=".self::$partnerKey;
        $objData['task_id']    = $data['task_id'];
        $objData['sms_code']   = $data['sms_code'];
        $objData['task_stage'] = $data['task_stage'];
        $objData['request_type'] = 'submit';
        $result = self::queryCode($url , $objData);
        if($result == 137){
            return true;
        }else{
            self::$errInfo = $result;
            return false;
        }
    }



    static public function queryCode($url , $objData) {
        $result = getHttp($url,$objData);
        $result = json_decode($result , true);
        if($result['code'] == 100){
            $objData['request_type'] = 'query';
            return self::queryCode($url , $objData);
        }elseif($result['code'] == 137 || $result['code'] == 2007){
            return 137;
        }else{
            return $result['message'];
        }
    }




    //刷新验证码
    static public function refreshCode($taskId) {
        $url = "https://api.shujumohe.com/octopus/task.unify.retry/v3?partner_code=".self::$partnerCode."&partner_key=".self::$partnerKey;  
        $objData['task_id'] = $taskId;
        $result = getHttp($url , $objData);
        $result = json_decode($result , true);
        if($result['code'] == 105){
            return true;
        }else{
            self::$errInfo = $result['message'];
            return false;
        }
    }


    //获取用户通话记录
    static public function getCallLog($taskId) {
        $url = "https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code=".self::$partnerCode."&partner_key=".self::$partnerKey;
        $objData['task_id'] = $taskId;
        $result = self::queryCallLog($url , $objData);
        if($result){
            return $result;
        }else{
            return false;
        }
    }




    static public function queryCallLog($url , $objData) {
        $result = getHttp($url,$objData);
        $result = json_decode($result , true);
        if($result['code'] == 0){
            return $result['data']['task_data'];
        }elseif($result['code'] == 137){
            return self::queryCallLog($url,$objData);
        }else{
            self::$errInfo = $result['message'];
            return false;
        }
    }





    static public function errorMsg($code) {
        $errorData = array(
            61000 => '网络错误',
            61001 => '用户名或者密码错误',
            61002 => '验证码错误',
            61004 => 'OP参数错误',
            61005 => '用户不存在',
            61006 => '参数错误',
            61007 => '不支持省份',
            61008 => '用户发送短信验证码受限',
            64000 => '其他错误',
            64001 => '用户当天输入错误密码次数已达上限',
            64002 => '用户短信密码发送次数当天已达上限',
            64004 => '登录前需要进行验证手机密令',
            64005 => '用户未实名制',
            64006 => '联通运营商系统忙',
            64007 => '联通运营商系统升级',
            64010 => '用户身份证信息验证失败',
        );
        return $errorData[$code];
    }


    static public function gerError() {
        return self::$errInfo;
    }

}
